﻿function ColumnReorderHandler(dt) {
	var _this = this;

	this.dragstart = function(e) {
		_this.draggedCell = $(e.target).parent('column-header').get(0);
		e.dataTransfer.setData("Text", e.target.textContent);
		e.dataTransfer.setDragImage($('<img>').get(0), 0, 0);
		return true;
	};
	
	this.dragenter = function(e) {return true;};
	
	this.dragleave = function(e) {
		$(e.target).parent('column-header').removeClass('dragged-over dragged-over-last');
		return true;
	};
	
	this.dragend = function(e) {
		_this.draggedCell = null;
	};
	
	this.dragover = function(e) {
		var ch = $(e.target).parent('column-header').get(0);
		if (ch!=null) {
			var v = (e.pageX - $(ch).position().left) > 0.5 * $(ch).width();
			$(ch).removeClass('dragged-over dragged-over-last');
			$(ch).addClass(v ? 'dragged-over-last': 'dragged-over');
		}
		
		if (e.preventDefault) e.dataTransfer.dropEffect = 'move';
		return false;
	};
	
	this.drop = function(e) {
		var th = $(e.target).parent('column-header').get(0);
		if (th != null && _this.draggedCell != null && _this.draggedCell != th) {
			var srcW = $(_this.draggedCell).width(), trgW = $(th).width();
			var src = [_this.draggedCell].concat(dt.getColumnCells(_this.draggedCell));
			var trg = [th].concat(dt.getColumnCells(th));
			var meth = (e.pageX - $(th).position().left) > 0.5 * $(th).width() ? 'after': 'before';

			for (var i=0;i<src.length;i++) {
				$(trg[i])[meth]($(src[i]));
				$(trg[i]).width(trgW);
				$(src[i]).width(srcW);
			}
			
			/*Array.prototype.swap = function(i,j) {
				var t = this[i];
				this[i] = this[j];
				this[j] = t;
			};
			
			dt.headerCells.swap($(_this.draggedCell).index(), $(th).index());
			*/
			
			var t = dt.headerCells[$(_this.draggedCell).index()];
			dt.headerCells[$(_this.draggedCell).index()] = dt.headerCells[$(th).index()];
			dt.headerCells[$(th).index()] = t;
			
			$(dt).resize();
			$(th).removeClass('dragged-over dragged-over-last');
		}
		
		_this.draggedCell = null;
		$(_this.draggedCell).removeClass('dragged-over dragged-over-last');
		return false;
	};
	
	dt.getHeaderCells().each(function() {
		new DragAndDropHandler(this, _this);
	});
	
}; 

function RowReorderHandler(dt) {
	var _this = this;
	this.sourceRow = null;
	this.previousTarget = null;

	this.dragstart = function(e) {
		_this.sourceRow = $(this).parent('tr').get(0);
		
		e.dataTransfer.setData("Text", e.target.textContent);
		e.dataTransfer.setDragImage($('<img>').get(0), 0, 0);
		return true;
	};
	
	this.dragenter = function(e) {
		if (e.preventDefault) {
			var r = $(e.target).parent('tr').get(0);
			if (r!=null && r != _this.previousTarget) {
				$(_this.previousTarget).removeClass('first-row-dragged-over');
				$(_this.previousTarget).prev().removeClass('dragged-over');
				if (r.previousSibling) {
					$(r).prev().addClass('dragged-over');
				} else {
					$(r).addClass('first-row-dragged-over');
				}
				_this.previousTarget = r;
			}
		}
		return true;
	};
	this.dragleave = function(e) {return true;};
	this.dragend = function(e) {return true;};
	
	this.dragover = function(e) {
		if (e.preventDefault) e.preventDefault();
		e.dataTransfer.dropEffect = 'move';
		return false;
	};

	this.drop = function(e) {
		var trgRow = $(e.target).parent('tr');
		
		trgRow.prev().removeClass('dragged-over');
		trgRow.removeClass('first-row-dragged-over');
		
		if (trgRow.get(0) != _this.sourceRow) {
			if (dt.getSelectedRows().size() > 1) {
				dt.getSelectedRows().each(function() {
					if (trgRow.prev()) {
						trgRow.prev().after(this);
					} else {
						$(this).insertBefore(trgRow);
					}
				});
			} else {
				trgRow.before( _this.sourceRow);
			}
		}
		_this.draggedCell = null;
		return false;
	};	
	
	dt.pageContentChanged.add(function() {
		dt.getDataRows().each(function() {
			new DragAndDropHandler(this, _this);
		});
	});
};